<?php

namespace app\index\controller;

use api\guoyou\Guoyou;
use sdk\Upload;
use think\Controller;
use think\helper\Time;
use think\Request;
use think\initialize;
use app\index\model\Center as Centers;

class Center extends Controller
{
    private $center;

    public function _initialize()
    {
        parent::_initialize();
        $this->center = new Centers;
    }
    /**
     * 显示资源列表
     *
     * @return \think\Response
     */
    public function index($id = 1)
    {
        $id = 1;
        $data = $this->center->indexdata($id);
        return view('center/index', ['data'=>$data]);
    }

    /**
     * 显示创建资源表单页.
     *
     * @return \think\Response
     */
    public function create()
    {
        //
    }

    /**
     * 保存新建的资源
     *
     * @param  \think\Request  $request
     * @return \think\Response
     */
    public function save(Request $request)
    {
        //
    }

    /**
     * 显示指定的资源
     *
     * @param  int  $id
     * @return \think\Response
     */
    public function read($id)
    {
        //
    }

    /**
     * 显示编辑资源表单页.
     *
     * @param  int  $id
     * @return \think\Response
     */
    public function edit($id)
    {
        //
    }

    /**
     * 保存更新的资源
     *
     * @param  \think\Request  $request
     * @param  int  $id
     * @return \think\Response
     */
    public function update(Request $request, $id)
    {
        $data = request()->put();
        array_shift($data);
        $file = request()->file('icon');
        if ($file) {
            $uploads = new Upload();
            $info = $file->getInfo('file');
            $img = $uploads->image($info);
            // 返回七牛云上的文件名
            $data['icon'] = 'http://'.config('qiniu.DOMAIN').'/'.$img;
        }

        $res = $this->center->updatedata($data);
        if ($res) {
            $this->success('保存成功', url('index/center/index'));
        }
        $this->error('编辑失败, 请重试');


    }

    /**
     * 删除指定资源
     *
     * @param  int  $id
     * @return \think\Response
     */
    public function delete($id)
    {
        //
    }

    public function pic()
    {
        $file = request()->file('file');
        $uploads = new Upload();
        $info = $file->getInfo('file');

        // 捕获异常
        try {
            // 返回qiniu上的文件名
            $image = $uploads->image($info);
        } catch (\Exception $e) {
            return json(['status' => 0, 'message' => $e->getMessage()]);
        }

        // 返回给ajax状态
        if ($image) {
            $data = [
                'status' => 1,
                'message' => 'OK',
                'data' => config('qiniu.DOMAIN') . '/' . $image,
            ];
            return json($data);
        } else {
            return json(['status' => 0, 'message' => '上传失败']);
        }
    }

    public function vip()
    {
        $id = 1;
        $data = $this->center->indexdata($id);
        return view('center/vip', ['data'=>$data]);
    }


}
